<?php
/**
 * Created by DRCOM.
 * User: GZX
 * Datetime: 2021-11-09 20:53
 */

namespace app\api\controller;

use think\Controller;
use think\Db;


class Hdk extends Controller
{
    public function commission()
    {

        $min_id = cache('min_id')?:1;

        $url = "http://v2.api.haodanku.com/get_current_commission?apikey=sylar&min_id={$min_id}&back=100";
        $contents = file_get_contents($url);
        $data = json_decode($contents,true);

        cache('min_id',$data['min_id']);
        foreach ($data['data'] as $item){
            $itemid = $item['itemid'];
            $item = item_detail($itemid);
//            dump($item);
        }

        echo $min_id;
    }

    public function get_index_activity_items()
    {
        $itemDetail = db('itemDetail')->select();

        $block = [
            [
                'name' => '大家电',
                'item' => $itemDetail
            ],
            [
                'name' => 'hello world',
                'item' => $itemDetail
            ],
            [
                'name' => 'hello world',
                'item' => $itemDetail
            ],
            [
                'name' => 'hello world',
                'item' => $itemDetail
            ],
        ];

        $result = [
            'code' => 200,
            'msg'  => '请求成功',
            'data' => [
                'block' => $block
            ]
        ];

        echo json_encode($result);
    }

    // 商品转链
    public function ratesurl()
    {
        $itemId = input('itemid');
        $title = input('title', '淘宝的商品');
        $data = [
            'apikey'      => 'sylar',
            'itemid'      => $itemId,
            'pid'         => 'mm_1520060136_2232550319_111181400478',
            'get_taoword' => 1,
            'tb_name'     => 'tb370655212',
            'title'       => $title,
        ];
        $url = "https://v2.api.haodanku.com/ratesurl";

        $result = file_post_contents($url, $data);
        echo $result;
        exit;
        file_put_contents('/tmp/ratesurl',$result);
        $content = json_decode($result, true);
        //淘礼金
        $tkmoney = db('itemDetail')->where('itemid',$itemId)->value('tkmoney');
        if($tkmoney>2){
            $tlj = get_tlj($itemId,1.8);
            if (isset($tlj['tpwd'])){
                $content['data']['taoword'] = $tlj['tpwd'];
            }
        }
        echo json_encode($content);
    }

    public function salesList(){
        $url = 'http://v2.api.haodanku.com/sales_list?';
        $data = [
            'apikey'      => 'sylar',
            'sale_type'      => 1,
            'cid'         => '0',
            'get_taoword' => 1,
            'min_id'     => '1',
            'back'       => 500,
            'item_type'=>1
        ];
        $query = http_build_query($data);
        $result = file_get_contents($url.$query);
        $result = (json_decode($result,true));
        if (isset($result['data'])){
            foreach ($result['data'] as $item){
                db('itemDetail')->insert($item,true);
            }
        }
    }


}